package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName DepartmentServiceImpl
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-10-10 18:21
 * @Version 1.0
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        //paramMap变成对象
        String jsonString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(jsonString, Department.class);

        if (department == null) {
            throw new YyghException(20001,"数据错误");
        }

        //查询科室是否存在  医院编号+科室编号
        Department existsDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        //判断
        if (existsDepartment != null) {//修改
            department.setId(existsDepartment.getId());
            department.setCreateTime(existsDepartment.getCreateTime());
            department.setUpdateTime(new Date());
        }else {//新增
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
        }

        departmentRepository.save(department);
    }

    @Override
    public Page<Department> selectPage(Integer pageNum, Integer limit, DepartmentQueryVo departmentQueryVo) {
        //排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");

        //分页
        Pageable pageable = PageRequest.of(pageNum - 1, limit, sort);

        //查询条件
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);

        //创建匹配器,使用查询条件
        ExampleMatcher matcher = ExampleMatcher
                .matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);
        Page<Department> pages = departmentRepository.findAll(example, pageable);
        return pages;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> result = new ArrayList<>();

        //1.根据医院编号查询所有科室
        Department department = new Department();
        department.setHoscode(hoscode);

        Example example = Example.of(department);
        List<Department> departmentList = departmentRepository.findAll(example);

        //2 根据bigCode分组
        Map<String,List<Department>> departmentMap = departmentList.stream().collect(
                Collectors.groupingBy(Department::getBigcode)
        );

        //3 遍历 map
        for (Map.Entry<String,List<Department>> entry : departmentMap.entrySet()){
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> departmentList1 = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(departmentList1.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department1:departmentList1){
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepcode(department1.getDepcode());
                departmentVo1.setDepname(department1.getDepname());
                //封装到 list集合
                children.add(departmentVo1);
            }
            //把小科室list集合放到大科室children里面
            departmentVo.setChildren(children);

            //放到最终result里面
            result.add(departmentVo);
        }

        //返回
        return result;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
